package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Recommend;
import com.example.demo.mapper.RecommendMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Resource
    RecommendMapper recommmendMapper;



    @PostMapping("/addRecommend")
    public Result<?> save(@RequestBody Recommend recommend){
        recommmendMapper.insert(recommend);
        return Result.success();
    }


    @PutMapping
    public Result<?> update(@RequestBody Recommend recommend){
        recommmendMapper.updateById(recommend);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        recommmendMapper.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/del/{user_id}/{book_id}")
    public Result<?> deleteRecommend(@PathVariable Integer user_id,@PathVariable Integer book_id){
        QueryWrapper<Recommend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user_id).eq("book_id",book_id);
        int res = this.recommmendMapper.delete(wrapper);
        return Result.success(res);
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(recommmendMapper.selectById(id));
    }

    @GetMapping("/exitsRecommend/{user_id}/{book_id}")
    public Result<?> exitsRecommend(@PathVariable Integer user_id,@PathVariable Integer book_id) {
        List<Recommend> res = recommmendMapper.selectRecommend(user_id, book_id);
        return Result.success(res);
    }

    @GetMapping("/select/{user_id}")
    public Result<?> getById(@PathVariable Integer user_id) {
        ArrayList list = recommmendMapper.selectRecommendBook(user_id);
        System.out.println(list);
        return Result.success(list);
    }

}
